'''
Created on 2011-3-6

@author: bnw
'''
import urllib2
from BeautifulSoup import BeautifulSoup, Tag
import re
from stocktable import stocklist
import sys

if __name__ == '__main__':
    reload(sys)
    sys.setdefaultencoding('gbk')
    for stock in stocklist:
        stockid = stock[0]
        print stockid
##        if stockid != '000422':
##            continue
        urlstr = "http://money.finance.sina.com.cn/corp/go.php/vCI_CirculateStockHolder/stockid/" + stockid + "/displaytype/30.phtml"
        page = urllib2.urlopen(urlstr)
        soup = BeautifulSoup(page)
        fpathstr = "F:/data/CirculateStockHolder/" + stockid + ".txt"
        f = open(fpathstr, 'w')
        table = soup('table', id = 'CirculateShareholderTable')[0]
        trs = table('tr')[1:]
        time = ''
        rank = ''
        name = ''
        quan = ''
        perc = '' 
        attr = ''
        for i in range(len(trs)):
            if i%14 == 0:
                if trs[i]('td')[1] != None:
                    time = trs[i]('td')[1].string.strip()
                else:
                    time = 'null'
                continue
            elif i%14 == 1 or i%14 == 2 or i%14==13:
                continue
            else:
##               print i
                if trs[i]('td')[0].div != None:
                    rank = trs[i]('td')[0].div.string.strip()
                else:
                    rank = 'null'
                if trs[i]('td')[1].div != None:
                    name = trs[i]('td')[1].div.string.strip()
                else:
                    name = 'null'
                if trs[i]('td')[2].div != None:
                    quan = trs[i]('td')[2].div.string.strip()
                else:
                    quan = 'null'
                if trs[i]('td')[3].div != None:
                    perc = trs[i]('td')[3].div.string.strip()
                else:
                    perc = 'null'
                if trs[i]('td')[4].div != None:
                    perc = trs[i]('td')[4].div.string.strip()
                else:
                    perc = 'null'
##            try:
##                name.encode('gbk')
##            except UnicodeEncodeError:
##                name = "XX"    
            f.write(time)
            f.write('\t')
            f.write(rank)
            f.write('\t')
            f.write(name)
            f.write('\t')
            f.write(quan)
            f.write('\t')
            f.write(perc)
            f.write('\t')
            f.write(attr)
            f.write('\n')           
        f.close()     
        
    
